<?php
session_save_path('sessions');
session_start();

include("include/dbUserTypes.php");

// Include our user class
include_once("include/user.php");

// Create a variable to store our user if we're validated
$user = null;

// Check if we're in a valid session. If not, go back to main login page.

if( !isset($_SESSION['user']) || $_SESSION['user'] == null ) {
}
else {
    $user = unserialize($_SESSION['user']);
}

// Connect to our database
include("include/dbconnection.php");

// Include our user class
include_once("include/user.php");

$homeid=$_GET['id'];

?>
<?php include("include/header.php"); ?>
<html>
<body>
  <div id="wrapper">
    <div id="header"> </div>
    <div id="left">
      <div id="logo">
        <h1>Home Sale</h1>
        <p>Only By Owner</p>
      </div>
      <div id="nav">
        <ul>
          <?php 

		 if(isset($_SESSION['user']))
			{
				?>

				<li>
						<a href="addAhomeForSale.php">Add a House for sale</a>
					</li>
					<li>
						<a href="updateAhome.php">Update House information</a>
					</li>
					<li >
						<a href="updateAhomePicture.php">Update House picture</a>
					<li class="important">
						<a href="searchhouse.php">Search House</a></li>
					<li>
						<a href="emailMenu.php">Mailbox</a>
					</li>
					<li>
						<a href="logout.php">Logout</a>
				</li>
			<?php
				}
				else{
				?>
					  <li ><a href="index.php">Home</a></li>
					  <li><a href="main_login.php">Login</a></li>
					  <li><a href="register.php">Register</a></li>
					  <li class="important"><a href="searchhouse.php">Search House</a></li>
					  <li><a href="logout.php">Logout</a></li>

				<?php
						}
					?>
        </ul>
      </div>
      <div id="news">
        <?php include("adgeneratorleft1.php");?>
        <div class="hr-dots"> </div>
        <?php include("adgeneratorleft2.php");?>
        <p class="more"><a href="index.php">more</a></p>
      </div>
      <div id="support">
      </div>
    </div>
    <div id="right">
      <h2>Search Results</h2>
      <div id="welcome">
<?php

$db_query = "SELECT 
    h.$TABLE_HOME_HOMEID, h.$TABLE_HOME_ADDRESS, h.$TABLE_HOME_STREETNAME, 
    h.$TABLE_HOME_CITY, h.$TABLE_HOME_COUNTRY, h.$TABLE_HOME_DISTRICT, h.$TABLE_HOME_ZONE,
	h.$TABLE_HOME_POSTALCODE, h.$TABLE_HOME_LOTSIZE, h.$TABLE_HOME_PROPERTYTYPE, h.$TABLE_HOME_YEARBUILT,
	h.$TABLE_HOME_HEATING, h.$TABLE_HOME_COOLING, h.$TABLE_HOME_PARKING, h.$TABLE_HOME_GARAGE,
	h.$TABLE_HOME_DRIVEWAY, h.$TABLE_HOME_NEIGHBOURHOOD, h.$TABLE_HOME_IMPROVEMENTS, h.$TABLE_HOME_EVALUATION,
	h.$TABLE_HOME_TAXES, h.$TABLE_HOME_UTILITYCOST, h.$TABLE_HOME_PRICE, h.$TABLE_HOME_COMMENTS,
	h.$TABLE_HOME_LISTINGDATE
    FROM $TABLE_HOME h 
    WHERE homeID='$homeid'";

$mediaPath = "SELECT mediaPath FROM hasmedia h, media m WHERE homeID='$homeid' AND h.mediaID = m.mediaID";

$resultSet = mysql_query($db_query);
$mediaresult = mysql_query($mediaPath);

if(mysql_num_rows($resultSet) == 0)
{
  echo "No matches found!";
}
else{ ?>

        <?php
        // Show all the users and add an activation link.
        while( $result = mysql_fetch_array($resultSet) ) {
            ?>
      <table>
      <tr><td><b>HomeID:</b></td><td><b><a href="displayHome.php?id=<?php echo $result[$TABLE_HOME_HOMEID]?>"><?php echo $result[$TABLE_HOME_HOMEID]?></a></b></td></tr>
			<tr><td><b>Location:</b></td></tr>
			<tr><td><?php echo $result[$TABLE_HOME_CITY].", ".$result[$TABLE_HOME_DISTRICT].", ".$result[$TABLE_HOME_COUNTRY]?></td></tr>
			<tr>
				<td><b>Size:</b></td>
				<td><?php echo $result[$TABLE_HOME_LOTSIZE]?></td>
			</tr>
			<tr><td><b>Property Type:</b></td><td>
        <?php 
		switch($result[$TABLE_HOME_PROPERTYTYPE]){
        case 1:
        {
          echo "Condo";
          break;
        }
        case 2:
        {
          echo "Apartment";
          break;
        }
        case 3:
        {
          echo "Single family";
          break;
        }
        case 4:
        {
          echo "Cottages";
          break;
        }
		}
        ?>
		</td><td><b>Year built:</b></td>
				<td><?php echo $result[$TABLE_HOME_YEARBUILT]?></td></tr>
			<tr><td><b>Heating:</b></td><td>
			<?php if($result[$TABLE_HOME_HEATING]){
					echo "Yes";
				}
				else
				{
					echo "No";
				}
			 ?></td><td><b>Cooling:</b></td><td><?php if($result[$TABLE_HOME_COOLING]){
					echo "Yes";
				}
				else
				{
					echo "No";
				}
			 ?></td></tr>
			<tr><td><b>Parking:</b></td><td><?php if($result[$TABLE_HOME_PARKING]){
					echo "Yes";
				}
				else
				{
					echo "No";
				}
			 ?></td><td><b>Driveway:</b></td><td><?php if($result[$TABLE_HOME_DRIVEWAY]){
				echo "Yes";
				}
					else
				{
					echo "No";
				}
			 ?></td></tr>
			<tr><td><b>Neighbourhood:</b></td><td><?php echo $result[$TABLE_HOME_NEIGHBOURHOOD] ?></td></tr>
			<tr><td><b>Improvements:</b></td><td><?php echo $result[$TABLE_HOME_IMPROVEMENTS] ?></td></tr>
			<tr>
				<td><b>Evaluation:</b></td>
				<td><?php echo $result[$TABLE_HOME_EVALUATION]?></td>
			</tr>
			<tr>
				<td><b>Utility Cost:</b></td>
				<td><?php echo $result[$TABLE_HOME_UTILITYCOST]?></td>
			</tr>
			<tr>
				<td><b>Price:</b></td>
				<td><?php echo $result[$TABLE_HOME_PRICE]?></td>
			</tr>
			<tr><td><b>Comments:</b></td><td><?php echo $result[$TABLE_HOME_COMMENTS]?></td></tr>
			<tr>
				<td><b>Posted:</b></td>
				<td><?php echo date("d/m/Y", $result[$TABLE_HOME_LISTINGDATE])?></td>
            </tr>
      </table>
	  <p><b>Pictures</b></p>
	  <?php
        // Show all the users and add an activation link.
        while( $mresult = mysql_fetch_array($mediaresult) ) {
            ?>
	  <img src="<?php echo $mresult['mediaPath']; ?>" alt="No pictures" width="320" height="222" />
            <?php
			}

      }// End while loop ?>
<?php
} // End else statement
    

	if(isset($_SESSION['user']))
	{	
			$findOwner = mysql_query("select * from list where homeID = $homeid");

			$row1 = mysql_fetch_array($findOwner);


			$currentUser = $user->getUserID();
			$senderID = $row1['userID'];

			?>

			<table>
			<form name="myForm" action="emailMenuReplyMessage.php" method="post">
            <input type="hidden" name="userID" value ="<?php echo $currentUser?>">
            <input type="hidden" name="senderID" value ="<?php echo $senderID?>">
            <input type="hidden" name="messageSubject" value ="<?php echo "Interested in this home (".$homeid.")"?>">
            <input type="hidden" name="messageContent" value ="">
            <tr>
              <td>
                <input type="submit" value="Send Message" />
              </td>
            </tr>
          </table>

<br/>
<br/>



<a href = "buyhome.php? id=<?php echo $homeid ?> userid=<?php echo $currentUser ?>">Buy this home </a>

<?php
	}
    // Close the connection
    mysql_close();
?>
      </div>
      <h3></h3>
      <div id="profile">
        <div id="corp">
          <?php include("adgeneratorbottomleft.php");?>
        </div>
        <div id="indu">
          <?php include("adgeneratorbottomright.php");?>
        </div>
      </div>
    </div>
    <div class="clear"> </div>
    <div id="spacer"> </div>
    <div id="footer">
      <div id="copyright">
        &copy; Template taken from http://www.freewebsitetemplates.com/
      </div>
	  <div id="footerline"></div>
    </div>	
  </div>
</body>
</html>
